package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.crypto.provider.PBEWithMD5AndDESCipher;
import com.sun.media.jfxmedia.control.VideoRenderControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-07-12
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    VodClient vodClient;
    @Autowired
    public void setVodClient(VodClient vodClient) {
        this.vodClient = vodClient;
    }

    // 根据课程id来删除小节
    @Override
    public void removeByCourseId(String courseId) {
        //根据课程id来查询所有所属该课程之下的云端视频资源id
        QueryWrapper<EduVideo> aliyunVideoList = new QueryWrapper<>();
        aliyunVideoList.eq("course_id",courseId);
        aliyunVideoList.select("video_source_id");
        List<EduVideo> aliyunVideoSourceIdList = baseMapper.selectList(aliyunVideoList);
        //填充数据
        List<String> videoSourceIdList = new ArrayList<>();
        for(EduVideo item:aliyunVideoSourceIdList){
            String videoSourceId = item.getVideoSourceId();
            if(!StringUtils.isEmpty(videoSourceId)){
                videoSourceIdList.add(videoSourceId);
            }
        }
        //调用vod服务删除远程视频
        if(videoSourceIdList.size() > 0){
            vodClient.deleteVideoList(videoSourceIdList);
        }

        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
